package com.dhcc.core.modules.system.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableName;
import com.dhcc.core.framework.base.entity.DataEntity;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.util.ArrayUtil;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.SpringContextHolder;
import com.dhcc.core.modules.system.cache.dept.DeptCache;
import com.dhcc.core.modules.system.cache.position.PositionCache;
import com.dhcc.core.modules.system.service.IUserService;

/**
 * 用户表
 * 
 * @ClassName: User
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:36:50
 */
@TableName("sys_user")
public class User extends DataEntity<User> {

    private static final long serialVersionUID = 1L;

    /**
     * 头像
     */
    private String avatar;
    /**
     * 账号
     */
    private String loginName;
    /**
     * 工号
     */
    private String userCode;
    /**
     * 密码
     */
    private String password;
    /**
     * md5密码盐
     */
    private String salt;
    /**
     * 名字
     */
    private String name;
    /**
     * 生日
     */
    private Date birthday;
    /**
     * 性别（1：男 2：女）
     */
    private Integer sex;
    /**
     * 电子邮件
     */
    private String email;
    /**
     * 电话
     */
    private String phone;
    /**
     * 主部门id
     */
    private Long deptId;
    /**
     * 状态(1：启用 2：冻结 3：删除）
     */
    private Integer status;
    /**
     * 排序
     */
    private Integer weight;
    /**
     * 推送clientId
     */
    private Integer clientId;
    /**
     * 职务id
     */
    private Long positionId;
    
    /**
     * 首字母与全拼
     */
    private String initials;
    private String pinyin;
    
    @TableField(exist = false)
    public String roleId;//所有角色字符串  [1],[997389205103529986],[2]

    @TableField(exist = false)
    public List<Long> deptIds; // 所有部门id
    @TableField(exist = false)
    public List<Long> roleIds; // 所有角色id
    @TableField(exist = false)
    public List<DeptUser> deptUsers;
    /**
     * 主部门名称
     */
    @TableField(exist = false)
    private String deptName;
    /**
     * 职务名称
     */
    @TableField(exist = false)
    private String positionName;

    private String xcode;

    public String getInitials() {
        return initials;
    }

    public void setInitials(String initials) {
        this.initials = initials;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    public Long getPositionId() {
        return positionId;
    }

    public void setPositionId(Long positionId) {
        this.positionId = positionId;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public Integer getClientId() {
        return clientId;
    }

    public void setClientId(Integer clientId) {
        this.clientId = clientId;
    }

    public List<Long> getDeptIds() {
        return deptIds;
    }

    public void setDeptIds(List<Long> deptIds) {
        this.deptIds = deptIds;
    }

    public List<Long> getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(List<Long> roleIds) {
        this.roleIds = roleIds;
    }

    public List<DeptUser> getDeptUsers() {
        return deptUsers;
    }

    public void setDeptUsers(List<DeptUser> deptUsers) {
        this.deptUsers = deptUsers;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getPositionName() {
        return positionName;
    }

    public void setPositionName(String positionName) {
        this.positionName = positionName;
    }
    

    public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

    public String getXcode() {
        return xcode;
    }

    public void setXcode(String xcode) {
        this.xcode = xcode;
    }

    /**
     * 填充用户部门角色信息
     * @Title: fill 
     * @Description: TODO
     * @return
     * @return: User
     */
    public User fill(){
        IUserService userService = SpringContextHolder.getBean(IUserService.class);
        List<DeptUser> dus = userService.selectDeptUserByUserId(this.getId());
        List<Long> roleIds = new ArrayList<>();
        List<Long> deptIds = new ArrayList<>();
        for (DeptUser du : dus) {
            deptIds.add(du.getDeptId());
            String rids = du.getRoleIds();
            if(CommonUtil.isNotEmpty(rids)){
                Long[] ids = ArrayUtil.stringformatToArray(rids, ",");
                roleIds.addAll(ArrayUtil.arrayToList(ids));
            }
        }
        this.setDeptIds(deptIds);
        this.setRoleIds(roleIds);
        this.setDeptUsers(dus);
        this.setDeptName(DeptCache.me().getDeptNameById(this.getDeptId()));
        this.setPositionName(PositionCache.me().getPositionNameById(this.getPositionId()));
        return this;
    }
    /**
     * 当前用户是否是admin
     * @Title: isAdmin 
     * @Description: TODO
     * @return
     * @return: boolean
     */
    public boolean isAdmin() {
        return CommonUtil.equals(this.getId(), Const.ADMIN_ID);
    }
}
